$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        beforeSend: function(jqXHR, settings)
        {
            jqXHR.url = settings.url;
            jqXHR.formDom = settings.formDom;
            jqXHR.bindDom = settings.bindDom;
            jqXHR.bindType = settings.bindType;
            jqXHR.bindFunc = settings.bindFunc; 

            console.log(jqXHR.bindType);
            if (typeof jqXHR.bindType != 'undefined') {
                $(jqXHR.bindDom).unbind(jqXHR.bindType);
            }
        },
        success: function(data, success, jqXHR) {
            // 处理事件绑定
            if (typeof jqXHR.bindType != 'undefined') {
                $(jqXHR.bindDom).bind(jqXHR.bindType, jqXHR.bindFunc);
            }

            if (typeof data.success.redirect != 'undefined') {
                location.href = data.success.redirect;
            }

            alert("操作成功");
        },
        statusCode: {
            422: function(jqXHR)
            {
                // 处理焦点
                if (typeof jqXHR.formDom != 'undefined') {
                    jqXHR.formDom.find('[name='+jqXHR.responseJSON.error.field+']').focus();
                }

                // 处理事件绑定
                if (typeof jqXHR.bindType != 'undefined') {
                    $(jqXHR.bindDom).bind(jqXHR.bindType, jqXHR.bindFunc);
                }

                // TODO 提示错误
                alert(jqXHR.responseJSON.error.message);

            },
            404: function(jqXHR)
            {
                // 处理事件绑定
                if (typeof jqXHR.bindType != 'undefined') {
                    $(jqXHR.bindDom).bind(jqXHR.bindType, jqXHR.bindFunc);
                }
                ga('send', 'exception', {
                    'exDescription': "404 页面找不到，" + jqXHR.url,
                    'exFatal': true
                });
            },
            405: function(jqXHR)
            {
                // 处理事件绑定
                if (typeof jqXHR.bindType != 'undefined') {
                    $(jqXHR.bindDom).bind(jqXHR.bindType, jqXHR.bindFunc);
                }
                ga('send', 'exception', {
                    'exDescription': "405 此操作不允许，" + jqXHR.url,
                    'exFatal': true
                });
            },
            500: function(jqXHR)
            {
                // 处理事件绑定
                if (typeof jqXHR.bindType != 'undefined') {
                    $(jqXHR.bindDom).bind(jqXHR.bindType, jqXHR.bindFunc);
                }
                ga('send', 'exception', {
                    'exDescription': "500 服务器内部错误，" + jqXHR.url,
                    'exFatal': true
                });
            },
            503: function(jqXHR)
            {
                // 处理事件绑定
                if (typeof jqXHR.bindType != 'undefined') {
                    $(jqXHR.bindDom).bind(jqXHR.bindType, jqXHR.bindFunc);
                }

                alert("系统维护中，请稍后再试");
            }
        }
});

$(function() {
    $('.delete').click(function() {
        var url = $(this).attr('data-url');
        var redirect = $(this).attr('data-redirect');
        $.ajax({
            url:url,
            type: 'delete',
            datatype: 'json',
            data: {redirect: redirect}
        });
    });
});


